package com.cdcas;

import java.util.concurrent.TimeUnit;
import java.util.concurrent.locks.Lock;
import java.util.concurrent.locks.ReentrantLock;

public class Ticket {
    private int count= 3;
    private Lock lock = new ReentrantLock();
    public void sale() {
        this.lock.lock();
        try {
            if (this.count > 0) {
                TimeUnit.SECONDS.sleep(1);
                System.out.println(Thread.currentThread().getName() + "卖票，剩余票数：" + this.count--);
            } else {
                System.out.println(Thread.currentThread().getName() + "票已卖完");
            }
        }catch (Exception e) {
            e.printStackTrace();
        } finally {
            this.lock.unlock();
        }
    }
}
